﻿using System;
using System.Collections.Generic;
using Lottery.App.Model;
using Lottery.App.DAL;

namespace Lottery.App.BLL
{
    /// <summary>
    /// 彩票统计管理类。
    /// </summary>
    public class LotteryManager
    {
        private RecordDao _dao = new RecordDao();

        public RecordInfo Update(RecordInfo record)
        {
            _dao.Update(record);

            _dao.CommitChanges();

            return record;
        }

        public RecordInfo Get(string issue)
        {
            return _dao.Get(issue); 
        }

        private const string URL = "http://www.kkcai.net/api.aspx?start={0}";
        private class RecordMin
        {
            public string Phase { get; set; }

            public string Result { get; set; }
        }
        public int AutoUpdate()
        {
            string url = String.Format(URL, _dao.GetCount());

            string resp = new Core.WebUtils().DoGet(url, null);

            List<RecordMin> list = null;

            try
            {
                list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RecordMin>>(resp);
            }
            catch
            {
                return -1;
            }

            if (list != null && list.Count > 0)
            {
                foreach (RecordMin item in list)
                {
                    this.Save(item.Phase, item.Result);
                }

                return list.Count;
            }

            return 0;
        }
        /// <summary>
        /// 添加一期开奖记录
        /// </summary>
        public void Save(string issue, string result)
        {
            RecordInfo r = new RecordInfo(issue, result);

            _dao.Save(r);

            GlobalData.Refresh(r);

            new A1Dao().UpdateRecord(r);
            new A2Dao().UpdateRecord(r);
            new A3Dao().UpdateRecord(r);
            new P2Dao().UpdateRecord(r);
            new P3Dao().UpdateRecord(r);
            new N2Dao().UpdateRecord(r);
            new N3Dao().UpdateRecord(r);
            new N4Dao().UpdateRecord(r);
            new N5Dao().UpdateRecord(r);
            
            new D1T2Dao().UpdateRecord(r);
            new B4Z3Dao().UpdateRecord(r);
            new B5Z4Dao().UpdateRecord(r);
            new B6Z5Dao().UpdateRecord(r);
        }

        private const string ID = "id";
        private const string MaxOmit = "MaxOmit";
        private const string CurrentOmit = "LastID";
        private const string Date = "MaxOmitQH";
        private const string TotalCount = "TotalCount";

        public object List(GameMode mode, OrderBy order)
        {
            string orderby = ID;
            bool asc = true;

            switch (order)
            {
                case OrderBy.ID: orderby = ID;
                    break;
                case OrderBy.MaxOmit: orderby = MaxOmit; asc = false;
                    break;
                case OrderBy.CurrentOmit: orderby = CurrentOmit;
                    break;
                case OrderBy.Date: orderby = Date; asc = false;
                    break;
                case OrderBy.TotalCount: orderby = TotalCount;
                    break;
                default:
                    break;
            }

            switch (mode)
            {
                case GameMode.A1: return new A1Dao().Order(orderby, asc);
                case GameMode.A2: return new A2Dao().Order(orderby, asc);
                case GameMode.A3: return new A3Dao().Order(orderby, asc);
                case GameMode.P2: return new P2Dao().Order(orderby, asc);
                case GameMode.P3: return new P3Dao().Order(orderby, asc);
                case GameMode.N2: return new N2Dao().Order(orderby, asc);
                case GameMode.N3: return new N3Dao().Order(orderby, asc);
                case GameMode.N4: return new N4Dao().Order(orderby, asc);
                case GameMode.N5: return new N5Dao().Order(orderby, asc);
                case GameMode.D1T2: return new D1T2Dao().Order(orderby, asc);
                case GameMode.B4Z3: return new B4Z3Dao().Order(orderby, asc);
                case GameMode.B5Z4: return new B5Z4Dao().Order(orderby, asc);
                case GameMode.B6Z5: return new B6Z5Dao().Order(orderby, asc);
                default:
                    return null;
            }
        }

        public object Search(GameMode mode, string key)
        {
            //key = Utility.Utils.Sort(key);

            switch (mode)
            {
                case GameMode.A3: return new A3Dao().Search(key);
                case GameMode.P3: return new P3Dao().Search(key);
                case GameMode.N2: return new N2Dao().Search(key);
                case GameMode.N3: return new N3Dao().Search(key);
                case GameMode.N4: return new N4Dao().Search(key);
                case GameMode.N5: return new N5Dao().Search(key);
                case GameMode.B4Z3: return new B4Z3Dao().Search(key);
                case GameMode.B5Z4: return new B5Z4Dao().Search(key);
                case GameMode.B6Z5: return new B6Z5Dao().Search(key);
                default:
                    break;
            }

            return null;
        }

        public void Reset()
        {
            new A1Dao().Analyze();// 3sec
            new A2Dao().Analyze();// 16sec
            new P2Dao().Analyze();// 13sec
            new A3Dao().Analyze();// 30sec
            new P3Dao().Analyze();// 16sec
            new N2Dao().Analyze();// 12sec
            new N3Dao().Reset();  // 3sec
            new N4Dao().Reset();  // 17sec
            new N5Dao().Reset();  // 75sec
            new D1T2Dao().Reset();//
            new B4Z3Dao().Reset();// 25sec
            new B5Z4Dao().Reset();// 95sec
            new B6Z5Dao().Reset();// 340sec
        }

        
    }
}
